package com.dongjiantong.app.controller.biz;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongjiantong.biz.model.Stables;
import com.dongjiantong.biz.service.StablesService;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.exceptions.OPServerException;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.farmrecords.service.FarmRecordsFileService;
import com.dongjiantong.openplatform.annotations.OpenBean;
import com.dongjiantong.openplatform.annotations.OpenMethod;
import com.dongjiantong.user.model.Farms;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.*;

/**
 * Created by ldf on 2017/11/17.
 */
@Slf4j
@OpenBean
public class StablesController {
    @Reference(version = "1.0.0")
    private StablesService stablesService;

    @Reference(version = "1.0.0")
    private FarmRecordsFileService farmRecordsFileService;
    /**
     * @param stables 存栏实体类
     * @return
     */
    @OpenMethod(key = "app.Stables.createStable",
            paramNames = {"stables"},
            httpMethods = RequestMethod.POST)
    public Response<Map> createStable(Stables stables) {
        stables.setCreated_at(new Date());
        if (stables.getFarm_id() == null) {
            throw new OPServerException(HttpCode.BAD_REQUEST, "缺失养殖场id");
        }

        int result = stablesService.insertSelective(stables);
        if (result == 0) {
            throw new OPServerException(HttpCode.DATA_CREATE_FAIL, "创建失败请稍后再试！");

        }
        Map map = new HashMap();

        map.put("data", "创建成功");
        return Response.ok(map);
    }

    /**
     * todo 更新新存栏信息
     *
     * @param stables
     * @return
     */
    @OpenMethod(key = "app.Stables.updateStable",
            paramNames = {"stables"},
            httpMethods = RequestMethod.POST)
    public Response<Map> updateStable(Stables stables) {
        stables.setUpdated_at(new Date());
        if (stables.getFarm_id() == null) {
            throw new OPServerException(HttpCode.BAD_REQUEST, "缺失养殖场id");
        }

        int result = stablesService.updateByPrimaryKeySelective(stables);
        if (result == 0) {
            throw new OPServerException(HttpCode.DATA_CREATE_FAIL, " 更新失败请稍后再试！");

        }
        Map map = new HashMap();

        map.put("data", "更新成功");
        return Response.ok(map);
    }

    @OpenMethod(key = "app.Stables.findById",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET)
    public Response<Stables> findById(Long id) {
        if (id == null) {
            throw new OPServerException(HttpCode.BAD_REQUEST, "参数出错");
        }
        Stables stables = stablesService.selectByPrimaryKey(id);
        if (stables == null) {
            throw new OPServerException(HttpCode.BAD_REQUEST, "参数不合法");
        }
        return Response.ok(stables);

    }


    /**
     * todo 删除存栏信息
     *
     * @param id
     * @return
     */
    @OpenMethod(key = "app.Stables.deleteStable",
            paramNames = {"id"},
            httpMethods = RequestMethod.POST)
    public Response<Map> deleteStable(Long id) {
        if (id == null) {
            throw new OPServerException(HttpCode.BAD_REQUEST, "缺失栏舍id");
        }

        int result = stablesService.deleteByPrimaryKey(id);
        if (result == 0) {
            throw new OPServerException(HttpCode.DATA_CREATE_FAIL, "删除失败请稍后再试");

        }
        Map map = new HashMap();

        map.put("data", "删除成功");
        return Response.ok(map);
    }

    /**
     * todo 根据养殖场ID分页查询该养殖场存栏情况
     *
     * @param farmId   养殖场ID
     * @param pageNum  页码
     * @param pageSize 一页条数
     * @return
     */
    @OpenMethod(key = "app.Stables.findByFarmId",
            paramNames = {"farmId", "pageNum", "pageSize"},
            httpMethods = RequestMethod.GET)
    public Response<PageInfo<Stables>> findByFarmId(Long farmId, int pageNum, int pageSize) {

        if (farmId == null) {
            throw new OPServerException(HttpCode.BAD_REQUEST, "缺失养殖场id");
        }
        Stables stables = new Stables();
        stables.setFarm_id(farmId);
        try {
            PageInfo<Stables> pageInfo = stablesService.selectPage(pageNum, pageSize, stables);
            return Response.ok(pageInfo);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new OPServerException(HttpCode.INTERNAL_SERVER_ERROR, "查询出错请稍后再试");
        }

    }


}
